package com.pfsoft.controller;/**
 * 文章管理控制器
 *
 * @author PF
 * Created by dell on 2017-09-30.
 */

import com.pfsoft.model.Article;
import com.pfsoft.query.ArticleQuery;
import com.pfsoft.service.IArticleManager;
import org.mybatis.spring.annotation.MapperScan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import validate.Groups;


/**
 * com.pfsoft.controller
 * 文章管理控制器
 * @author PF
 * @create 2017-09-30 9:41
 **/
@RestController
public class ArticleController {

    @Autowired
    private IArticleManager articleManager;

    private Logger logger= LoggerFactory.getLogger(ArticleController.class);

    @RequestMapping(value = "/articles",produces = "application/json",method = RequestMethod.POST,consumes = "application/json")
    public ResponseEntity<Page<Article>> getArticles(@Validated({Groups.Default.class}) @RequestBody ArticleQuery queryCondition, BindingResult bindingResult)
    {
        ResponseEntity<Page<Article>> responseEntity;
        if(bindingResult.hasErrors()) {
            return new ResponseEntity<Page<Article>>(HttpStatus.BAD_REQUEST);
        }
        try {
                Page<Article> articles = articleManager.getArticles(queryCondition);
                responseEntity=new ResponseEntity<Page<Article>>(articles,HttpStatus.OK);
        } catch (Exception e) {
            logger.error("获取文章列表发生错误",e);
             responseEntity=new ResponseEntity<Page<Article>>(HttpStatus.BAD_REQUEST);
        }
        return responseEntity;
    }

}
